package com.anolesoft.yr.mbus.prod.base.service.impl;

import com.anolesoft.yr.mbus.prod.base.dto.*;
import com.anolesoft.yr.mbus.prod.base.mapper.ProdBaseServiceMapper;
import com.anolesoft.yr.mbus.prod.base.mapper.ProdBaseServiceValueMapper;
import com.anolesoft.yr.mbus.prod.base.service.ProdBaseServiceService;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceQueryNameVO;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceQueryVO;
import com.anolesoft.yr.mbus.prod.base.vo.ProdBaseServiceValueQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@Service
public class ProdBaseServiceServiceImpl implements ProdBaseServiceService {

    private static final String CODE_EXIST = "CODE_EXIST";
    private static final String NAME_EXIST = "NAME_EXIST";
    private static final String SUCCESS = "SUCCESS";
    private static final String EXCEPTION = "EXCEPTION";

    @Autowired
    ProdBaseServiceMapper prodBaseServiceMapper;
    @Autowired
    ProdBaseServiceValueMapper prodBaseServiceValueMapper;

    //新增服务项基础信息表
    @Override
    @Transactional
    public String saveProdBaseService(ProdBaseServiceSaveDTO prodBaseServiceSaveDTO) throws Exception {
        try {
            int count = 0;

            count = prodBaseServiceMapper.queryProdBaseServiceByCode(prodBaseServiceSaveDTO.getBaseServiceCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            count = prodBaseServiceMapper.queryProdBaseServiceByName(prodBaseServiceSaveDTO.getBaseServiceName());
            if (count > 0) {
                return NAME_EXIST;
            }

            List prodBaseServiceSaveDTOList = prodBaseServiceSaveDTO.getProdBaseServiceValueSaveDTO();
            for (int i = 0; i < prodBaseServiceSaveDTOList.size(); i++) {
                ProdBaseServiceValueSaveDTO prodBaseServiceValueSaveDTO = (ProdBaseServiceValueSaveDTO) prodBaseServiceSaveDTOList.get(i);
                prodBaseServiceValueMapper.saveProdBaseServiceValue(prodBaseServiceValueSaveDTO);
            }

            int rows = prodBaseServiceMapper.saveProdBaseService(prodBaseServiceSaveDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //修改服务项基础信息表
    @Override
    @Transactional
    public String updateProdBaseService(Map<String, Object> maps) throws Exception {
        try {
            List prodBaseServiceValueSaveDTOList = (List) maps.get("save");
            ProdBaseServiceUpdateDTO prodBaseServiceUpdateDTO = (ProdBaseServiceUpdateDTO) maps.get("update");
            Map deleteMap = (Map) maps.get("delete");

            int count = 0;

            ProdBaseServiceQueryVO prodBaseServiceQueryVO = prodBaseServiceMapper.queryProdBaseServiceByUid(prodBaseServiceUpdateDTO.getObjuid());
            count = prodBaseServiceMapper.query2ProdBaseServiceByCode(prodBaseServiceUpdateDTO.getBaseServiceCode(), prodBaseServiceQueryVO.getBaseServiceCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            count = prodBaseServiceMapper.query2ProdBaseServiceByName(prodBaseServiceUpdateDTO.getBaseServiceName(), prodBaseServiceQueryVO.getBaseServiceName());
            if (count > 0) {
                return NAME_EXIST;
            }

            List prodBaseServiceUpdateDTOList = prodBaseServiceUpdateDTO.getProdBaseServiceValueUpdateDTO();
            for (int i = 0; i < prodBaseServiceUpdateDTOList.size(); i++) {
                ProdBaseServiceValueUpdateDTO prodBaseServiceValueUpdateDTO = (ProdBaseServiceValueUpdateDTO) prodBaseServiceUpdateDTOList.get(i);
                prodBaseServiceValueMapper.updateProdBaseServiceValue(prodBaseServiceValueUpdateDTO);
            }

            for (int i = 0; i < prodBaseServiceValueSaveDTOList.size(); i++) {
                ProdBaseServiceValueSaveDTO prodBaseServiceValueSaveDTO = (ProdBaseServiceValueSaveDTO) prodBaseServiceValueSaveDTOList.get(i);
                prodBaseServiceValueMapper.saveProdBaseServiceValue(prodBaseServiceValueSaveDTO);
            }

            String prodBaseServiceDeleteUid = deleteMap.get("prodBaseServiceDeleteUid").toString();
            List prodBaseServiceValueDeleteDTOList = (List) deleteMap.get("prodBaseServiceValueDeleteDTOList");
            if (prodBaseServiceValueDeleteDTOList.size() > 0) {
                int rows = prodBaseServiceValueMapper.deleteProdBaseServiceValue2(prodBaseServiceDeleteUid, prodBaseServiceValueDeleteDTOList);
            }

            int rows = prodBaseServiceMapper.updateProdBaseService(prodBaseServiceUpdateDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //删除服务项基础信息表
    @Override
    @Transactional
    public String deleteProdBaseService(List<ProdBaseServiceDeleteDTO> prodBaseServiceDeleteDTOList) throws Exception {
        try {
            int rows = 0;
            for (int i = 0; i < prodBaseServiceDeleteDTOList.size(); i++) {
                ProdBaseServiceDeleteDTO prodBaseServiceDeleteDTO = prodBaseServiceDeleteDTOList.get(i);
                rows += prodBaseServiceMapper.deleteProdBaseService(prodBaseServiceDeleteDTO.getObjuid());

                prodBaseServiceValueMapper.deleteProdBaseServiceValue(prodBaseServiceDeleteDTO.getObjuid());
            }

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //根据主键查询服务项基础唯一数据
    @Override
    public Map<String, Object> queryProdBaseServiceByUid(String uid) throws Exception {
        ProdBaseServiceQueryVO prodBaseServiceQueryVO = prodBaseServiceMapper.queryProdBaseServiceByUid(uid);
        List<ProdBaseServiceValueQueryVO> prodBaseServiceValueQueryVOList = prodBaseServiceValueMapper.queryProdBaseServiceValueByUid(uid);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("prodBaseServiceQueryVO", prodBaseServiceQueryVO);
        maps.put("prodBaseServiceValueQueryVOList", prodBaseServiceValueQueryVOList);
        return maps;
    }


    //根据条件查询服务项基础所有数据
    @Override
    public Map<String, Object> queryProdBaseServiceAll(String baseServiceName, int page, int limit) throws Exception {
        int count = prodBaseServiceMapper.queryProdBaseServiceAllCount(baseServiceName);
        List<ProdBaseServiceQueryVO> prodBaseServiceQueryVOList = prodBaseServiceMapper.queryProdBaseServiceAll(baseServiceName, (page - 1) * limit, limit);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("count", count);
        maps.put("prodBaseServiceQueryVOList", prodBaseServiceQueryVOList);
        return maps;
    }

    //查询基础服务项名称
    @Override
    public List<ProdBaseServiceQueryNameVO> queryBaseServiceName() throws Exception {
        List<ProdBaseServiceQueryNameVO> prodBaseServiceQueryNameVOList = prodBaseServiceMapper.queryBaseServiceName();
        return prodBaseServiceQueryNameVOList;
    }

}
